﻿using System;
using NATTraversal;
using Open.Nat;
using UnityEngine;
using UnityEngine.Networking;

// Token: 0x020000AD RID: 173
public class NATHelperTester : MonoBehaviour
{
	// Token: 0x06000406 RID: 1030 RVA: 0x00021270 File Offset: 0x0001F470
	private void Awake()
	{
		LogFilter.currentLogLevel = 1;
		this.natHelper = base.GetComponent<NATHelper>();
		this.natHelper.findNatDevice(null);
		this.natHelper.StartCoroutine(this.natHelper.connectToNATFacilitator());
		NetworkTransport.Init();
	}

	// Token: 0x06000407 RID: 1031 RVA: 0x000212AC File Offset: 0x0001F4AC
	private void OnGUI()
	{
		if (!this.natHelper.isConnectedToFacilitator)
		{
			GUI.enabled = false;
		}
		if (!this.natHelper.isPunchingThrough && !this.natHelper.isListeningForPunchthrough)
		{
			if (GUI.Button(new Rect(10f, 10f, 150f, 40f), "Listen for Punchthrough"))
			{
				Debug.Log("Listening for punchthrough");
				this.natHelper.StartCoroutine(this.natHelper.startListeningForPunchthrough(new Action<int, ulong>(this.onHolePunchedServer)));
			}
		}
		else if (this.natHelper.isListeningForPunchthrough && GUI.Button(new Rect(10f, 10f, 150f, 40f), "Stop Listening"))
		{
			this.natHelper.StopListeningForPunchthrough();
		}
		if (this.natHelper.isListeningForPunchthrough)
		{
			GUI.Label(new Rect(170f, 10f, 170f, 20f), "Host GUID");
			GUI.TextField(new Rect(170f, 30f, 200f, 20f), this.natHelper.guid.ToString());
		}
		else if (!this.natHelper.isPunchingThrough)
		{
			if (GUI.Button(new Rect(10f, 60f, 150f, 40f), "Punchthrough"))
			{
				Debug.Log("Trying to punch through");
				this.natHelper.StartCoroutine(this.natHelper.punchThroughToServer(this.hostGUID, new Action<int, int, bool>(this.onHolePunchedClient)));
			}
			GUI.Label(new Rect(170f, 60f, 170f, 20f), "Host GUID");
			this.hostGUIDString = GUI.TextField(new Rect(170f, 80f, 200f, 20f), this.hostGUIDString);
			ulong.TryParse(this.hostGUIDString, out this.hostGUID);
		}
		if (GUI.Button(new Rect(10f, 110f, 150f, 40f), "Forward port"))
		{
			Debug.Log("Forward port: " + this.directConnectPort);
			this.natHelper.mapPort((int)this.directConnectPort, (int)this.directConnectPort, 0, NATTraversal.Protocol.Both, "NAT Test", new Action<Mapping, bool>(this.onPortMappingDone));
		}
		if ((this.natHelper.isForwardingPort || !this.natHelper.isDoneFindingNATDevice) && GUI.Button(new Rect(10f, 160f, 150f, 40f), "Stop port forwarding"))
		{
			this.natHelper.stopPortForwarding();
		}
	}

	// Token: 0x06000408 RID: 1032 RVA: 0x00021582 File Offset: 0x0001F782
	private void onHolePunchedServer(int portToListenOn, ulong clientGUID)
	{
		Debug.Log(string.Concat(new object[]
		{
			"Start a server listening on this port: ",
			portToListenOn,
			" for client ",
			clientGUID
		}));
	}

	// Token: 0x06000409 RID: 1033 RVA: 0x000215B8 File Offset: 0x0001F7B8
	private void onHolePunchedClient(int clientPort, int serverPort, bool success)
	{
		if (success)
		{
			Debug.Log(string.Concat(new object[]
			{
				"Start a socket on ",
				clientPort,
				" and connect to the server on ",
				serverPort
			}));
		}
		else
		{
			Debug.Log("Punchthrough failed.");
		}
	}

	// Token: 0x0600040A RID: 1034 RVA: 0x0002160C File Offset: 0x0001F80C
	private void onPortMappingDone(Mapping mapping, bool isError)
	{
		if (isError)
		{
			Debug.Log("Port mapping failed");
		}
		else
		{
			Debug.Log(string.Concat(new object[]
			{
				"Port ",
				mapping.PublicPort,
				" mapped (",
				mapping.Protocol,
				")"
			}));
		}
	}

	// Token: 0x04000566 RID: 1382
	public ushort directConnectPort = 7777;

	// Token: 0x04000567 RID: 1383
	private ulong hostGUID;

	// Token: 0x04000568 RID: 1384
	private string hostGUIDString = string.Empty;

	// Token: 0x04000569 RID: 1385
	private NATHelper natHelper;
}
